package com.livew.framework.base.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.github.pagehelper.PageHelper;
import com.livew.framework.base.dao.BaseMapper;
import com.livew.framework.base.model.BaseModel;
import com.livew.framework.base.service.BaseService;
import com.livew.framework.exception.XTBusinessException;
import com.livew.framework.page.PagedBeanUtil;
import com.livew.framework.page.PagedResult;
import com.livew.framework.utils.DeleteType;
import com.livew.framework.utils.XTCurrentUser;
import com.livew.framework.utils.XTListUtils;

public abstract class BaseServiceImpl<T extends BaseModel> implements BaseService<T> {

	private BaseMapper<T> baseMapper;

	public BaseServiceImpl(BaseMapper<T> baseMapper) {
		this.baseMapper = baseMapper;
	}

	@Override
	public T getByPrimaryKey(Long id) throws XTBusinessException {
		if (id == null) {
			throw new XTBusinessException("参数错误");
		}
		T t = baseMapper.selectByPrimaryKey(id);
		return t;
	}

	@Override
	public int delByPrimaryKey(T t) throws XTBusinessException {
		if (t == null || t.getId() == null) {
			throw new XTBusinessException("参数错误");
		}
		try {
			t.setIsDel(DeleteType.IS_DELETE);
			t.setUpdateTime(new Date());
			t.setUpdateUserId(XTCurrentUser.getCurrentUserId());
			baseMapper.updateByPrimaryKeySelective(t);
		} catch (Exception e) {
			e.printStackTrace();
			throw new XTBusinessException("参数错误");
		}
		return 0;
	}

	@Override
	public void save(T record) throws XTBusinessException {
		if (record == null || record.getId() != null) {
			throw new XTBusinessException("参数错误");
		}
		Date date = new Date();
		record.setCreateTime(date);
		record.setCreateUserId(XTCurrentUser.getCurrentUserId());
		baseMapper.insert(record);

	}

	@Override
	public void saveSelective(T record) throws XTBusinessException {
		if (record == null || record.getId() != null) {
			throw new XTBusinessException("参数错误");
		}
		Date date = new Date();
		record.setCreateTime(date);
		record.setCreateUserId(XTCurrentUser.getCurrentUserId());
		baseMapper.insertSelective(record);
	}

	@Override
	public void updateByPrimaryKeySelective(T record) throws XTBusinessException {
		if (record == null || record.getId() == null) {
			throw new XTBusinessException("参数错误");
		}
		Date date = new Date();
		record.setUpdateTime(date);
		record.setUpdateUserId(XTCurrentUser.getCurrentUserId());
		record.setIsDel(null);
		baseMapper.updateByPrimaryKeySelective(record);
	}

	@Override
	public void updateByPrimaryKey(T record) throws XTBusinessException {
		if (record == null || record.getId() == null) {
			throw new XTBusinessException("参数错误");
		}
		T old = baseMapper.selectByPrimaryKey(record.getId());
		if (old == null) {
			throw new XTBusinessException("参数错误");
		}
		Date date = new Date();
		record.setUpdateTime(date);
		record.setUpdateUserId(XTCurrentUser.getCurrentUserId());
		record.setIsDel(null);
		baseMapper.updateByPrimaryKeySelective(record);
	}

	@Override
	public List<T> getListByCondition(T record) {
		if (record == null) {
			return null;
		}
		return baseMapper.selectListByCondition(record);
	}

	@Override
	public Map<Long, T> getMapByIdList(List<Long> ids) {
		if (XTListUtils.isEmpty(ids)) {
			return null;
		}
		List<T> list = baseMapper.selectByIdList(ids);
		if (XTListUtils.isEmpty(list)) {
			return null;
		}
		Map<Long, T> map = new HashMap<>();
		for (T t : list) {
			map.put(t.getId(), t);
		}
		return map;
	}

	@Override
	public List<T> getChcekInfoByCondition(T record) {
		List<T> st = baseMapper.selectListByCondition(record);
		return st;
	}

	@Override
	public void updateStatus(T t) throws XTBusinessException {
		if (t == null || t.getId() == null) {
			throw new XTBusinessException("参数错误");
		}
		t.setUpdateTime(new Date());
		t.setUpdateUserId(XTCurrentUser.getCurrentUserId());
		baseMapper.updateByPrimaryKeySelective(t);
	}

	@Override
	public PagedResult<T> findPagination(T t) {
		Integer pageNo = t.getPageNo() == null ? 1 : t.getPageNo();
		Integer pageSize = t.getPageSize() == null ? 15 : t.getPageSize();
		PageHelper.startPage(pageNo, pageSize);
		List<T> list = baseMapper.selectPagination(t);
		PagedResult<T> page = PagedBeanUtil.toPagedResult(list);
		return page;
	}

	@Override
	public List<T> getAll() {
		
		return baseMapper.selectAll();
	}

}
